
const fs = require('fs');
const chalk = require('chalk');
const config = require('./config');
const path = require('path');
const SentryCli = require('@sentry/cli');
/**
 * 清理js中的sourceMap注释
 * @param {string} file
 */
function cleanupMapComment(file){
  const fileName = path.basename(file);
  if(/\.js$/.test(fileName)){
    const reg = new RegExp(`//#\\s+sourceMappingURL=${fileName}.map\\s*$`)
    fs.writeFileSync(file,fs.readFileSync(file,'utf-8').replace(reg,''));
  }
}
/**
 * 清理js中的sourceMap注释
 * @param {string} file
 */
async function uploadSourceMap(){
  console.log(chalk.yellow('[Sentry] 正在上传SourceMap...'));
  sentryCli = new SentryCli(null, {
    ...config.sentry,
    silent:true,
  });
  const releaseVersion = config.sentry.releasesVersion;
  await sentryCli.releases.new(releaseVersion);
  await sentryCli.releases.uploadSourceMaps(releaseVersion,{
    include:[config.projectDist],
    rewrite:true,
    urlPrefix:`~${config.tupload.baseUrl}`
  });
  await sentryCli.releases.finalize(releaseVersion);
  console.log(chalk.green(`[Sentry] 文件夹："${path.relative(path.join(config.projectPath,'..'),config.projectDist)}"内所有SourceMap上传完成`));
}
module.exports = {
  cleanupMapComment,
  uploadSourceMap,
}